package com.zccx.eth.rest;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.http.MethodType;
import com.zccx.eth.model.CzfInfo;
import com.zccx.eth.service.ICzfInfoService;
import com.zccx.eth.util.Result;
import com.zccx.eth.util.object.StringUtils;
import com.zccx.eth.utils.SMSCode;
import com.zccx.eth.utils.SMSCodeCache;
import com.zccx.eth.utils.SendSmsByAliyun;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Log4j
@RestController
public class InfoController {
    @Autowired
    private ICzfInfoService czfInfoService;
    @Autowired
    private AliConfig config;

    @PostMapping("/info/addProject")
    public Result addProject(@RequestBody CzfInfo info){
        try{
            if(StringUtils.isEmpty(info.getAddress())){
                return new Result(false,"房屋地址不能为空");
            }
            if(StringUtils.isEmpty(info.getYt())){
                return new Result(false,"租赁用途不能为空");
            }
            if(StringUtils.isEmpty(info.getCzf())){
                return new Result(false,"出租方不能为空");
            }
            if(StringUtils.isEmpty(info.getZpf())){
                return new Result(false,"承租方不能为空");
            }
            if(StringUtils.isEmpty(info.getZpflx())){
                return new Result(false,"承租方类型不能为空");
            }
            if(StringUtils.isEmpty(info.getStarttime())){
                return new Result(false,"开始时间不能为空");
            }
            if(StringUtils.isEmpty(info.getEndtime())){
                return new Result(false,"结束时间不能为空");
            }
            if(StringUtils.isEmpty(info.getPhone())){
                return new Result(false,"手机号不能为空");
            }
            if(StringUtils.isEmpty(info.getBano())){
                return new Result(false,"备案编号不能为空");
            }
            czfInfoService.saveInfo(info);
            return new Result("保存成功");
        }catch (Exception e){
            log.error("保存信息失败",e);
            return new Result(false,"保存信息失败");
        }
    }

    @GetMapping("/info/findInfo/{phone}/{code}")
    public Result findInfo(@RequestParam(value = "bano",required = false)String bano,@RequestParam(value = "zpf",required = false)String zpf,@PathVariable("phone")String phone,@PathVariable("code")String code){
        try {
            if (StringUtils.isEmpty(bano) && StringUtils.isEmpty(zpf)) {
                return new Result(false, "备案编号或承租方姓名不能为空");
            }
            String oldCode = SMSCodeCache.getInstance().getCode(phone);
            if(!code.equals(oldCode)){
                return new Result(false, "验证码不正确");
            }
//            SMSCodeCache.getInstance().removeCode(phone);
            CzfInfo info = czfInfoService.searchInfo(bano,zpf);
            if(info == null){ //未查询到相关信息
                return new Result(false, "1001");
            }
            return new Result(info);
        }catch (Exception e){
            log.error("查询信息失败",e);
            return new Result(false,"查询信息失败");
        }
    }

    @GetMapping("/info/getInfo/{id}")
    public Result getInfo(@PathVariable("id")int id){
        try {
            CzfInfo info = czfInfoService.getOne(id);
            if(info == null){
                return new Result(false, "没有备案信息");
            }
            return new Result(info);
        }catch (Exception e){
            log.error("查询信息失败",e);
            return new Result(false,"查询信息失败");
        }
    }

    @GetMapping("/info/getList/{pageNum}/{pageSize}")
    public Result getList(@PathVariable("pageNum")int pageNum,@PathVariable("pageSize")int pageSize,@RequestParam("search")String search){
        try{
            Page<CzfInfo> all = czfInfoService.getList(pageNum,pageSize,search);
            return new Result(all);
        }catch (Exception e){
            log.error("获取列表失败",e);
            return new Result(false,"获取列表失败");
        }
    }
    @GetMapping("/info/getSMSCode/{phone}")
    @ResponseBody
    public Result getSMSCode(@PathVariable("phone")String phone){
        try {
            String code = new SMSCode().getSMSCode();
            //组装请求对象
            SendSmsRequest request = new SendSmsRequest();
            //使用post提交
            request.setMethod(MethodType.POST);
            request.setPhoneNumbers(phone);
            request.setSignName("新丐帮");
            request.setTemplateCode(config.getTemplateCode().trim());
            request.setTemplateParam("{\"code\":\"" + code + "\"}");//短信模板中的变量；数字需要转换为字符串；个人用户每个变量长度必须小于15个字符。"
            SendSmsByAliyun.sendSms(request,config);
            log.info("发送验证码短信:" + phone);
            log.info("发送验证码短信:" + code);
            SMSCodeCache.getInstance().putCode(phone, code);
            return new Result("发送验证码成功");
        }catch (Exception e){
            log.error("发送验证码失败",e);
            return new Result(false,"发送验证码失败");
        }
    }

    @GetMapping("/info/del/{id}")
    public Result del(@PathVariable("id")int id){
        try{
            czfInfoService.delInfo(id);
            return new Result("删除成功");
        }catch (Exception e){
            log.error("删除失败",e);
            return new Result(false,"删除失败");
        }
    }
}
